package com.work.libtest2.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.work.libtest2.entity.ExtraWorkType;
import com.work.libtest2.entity.R;
import com.work.libtest2.service.ExtraWorkTypeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * ClassName: ExtraWorkTypeController
 * Package: com.work.libtest2.controller
 * Description:
 *
 * @Author
 * @Create 2024/6/10 14:23
 * @Version 1.0
 */
@RestController
@Slf4j
@RequestMapping("/extraWorkType")
public class ExtraWorkTypeController {

    @Resource
    private ExtraWorkTypeService extraWorkTypeService;

    /**
     * 获取所有的加班类型
     * */

    @GetMapping("{currentPage}/{pageSize}")
    public R getPage(@PathVariable Integer currentPage,@PathVariable  Integer pageSize,ExtraWorkType extraWorkType){
        IPage<ExtraWorkType> page = extraWorkTypeService.getPage(currentPage,pageSize,extraWorkType);
        if (currentPage>page.getPages()){
            page = extraWorkTypeService.getPage((int) page.getPages(),pageSize,extraWorkType);
        }
        return new R(true,page);
    }

    /**
     * 增加加班类型
     * */

    @PostMapping
    public R addExtraWorkType(@RequestBody ExtraWorkType extraWorkType){
        int i = extraWorkTypeService.addEWType(extraWorkType);
        return new R(i>0,i>0?"添加成功":"添加失败");
    }
    /**
     * 根据id查找加班类型
     * */

    @GetMapping("{ewTypeId}")
    public R getEWById(@PathVariable Integer ewTypeId){
        ExtraWorkType extraWorkType = extraWorkTypeService.getEWById(ewTypeId);
        return new R(true,extraWorkType);
    }

    /**
     * 修改加班类型2
     * */

    @PutMapping
    public R updateEWById(@RequestBody ExtraWorkType extraWorkType){
        int i = extraWorkTypeService.updateEWById(extraWorkType);
        return new R(i>0);
    }
    /**
     * 删除部门
     * */
    @DeleteMapping("{ewTypeId}")
    public R deleteById(@PathVariable Integer ewTypeId){
        int i = extraWorkTypeService.deleteById(ewTypeId);
        return new R(i>0);
    }
    /**
     * 获取全部
     * */
    @GetMapping("/getAllEwType")
    public R getAll(){
       List<ExtraWorkType> extraWorkTypeList =  extraWorkTypeService.getAll();
       return new R(true,extraWorkTypeList);
    }
}
